package com.sias.realestate.web.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sias.realestate.web.model.Employee;
import com.sias.realestate.web.service.EmployeeService;
import com.sias.realestate.web.util.FinalVarUtil;
import com.sias.realestate.web.util.ViewModelUtil;
import com.sias.realstate.web.pagemodel.PageModel;

@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {
	
	@Resource
	private EmployeeService employeeService;
	@RequestMapping(value = "/getAllEmployee", method = RequestMethod.GET)
	public ModelAndView getAllEmployee(HttpServletRequest request)  {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		List<Employee> list = employeeService.selectList();
		String th_list = FinalVarUtil.employeeTh; 
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("employee/employeeList");
		return mv;		
	}
	
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：删除员工 
	 * 创建日期：2016年12月15日下午6:13:42
	 */
	@RequestMapping(value = "/deleteEmployee", method = RequestMethod.GET)
	public ModelAndView deleteEmployee(HttpServletRequest request,Integer eid)  {
		ModelAndView mv = new ModelAndView("redirect:getAllEmployee");
	//	PageModel pageModel = new PageModel();
		if(eid!=null){
			employeeService.delete(eid);
		}
//		List<Employee> list = employeeService.selectList();
//		String th_list = FinalVarUtil.employeeTh; 
//		pageModel.setTh_list(th_list);
//		mv.addObject("pageModel", pageModel);
//		mv.addObject("list", list);
//		mv.setViewName("employee/employeeList");
		return mv;		
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：进入添加新员工界面 
	 * 创建日期：2016年12月15日下午6:13:42
	 */
	@RequestMapping(value = "/addEmployeeJsp", method = RequestMethod.GET)
	public ModelAndView addEmployeeJsp(HttpServletRequest request)  {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("employee/addemployee");
		return mv;		
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：添加新员工
	 * 创建日期：2016年12月15日下午6:13:42
	 */
	@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
	public ModelAndView addEmployee(HttpServletRequest request,Employee employee)  {
		ModelAndView mv = new ModelAndView();
		employee.seteStatus("1");
		employeeService.insert(employee);
		request.setAttribute("successInfo", "添加员工信息成功！");
		request.setAttribute("page_code", "allemployee");
		mv.setViewName("/success-info");
		return mv;		
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：进入修改员工信息界面 
	 * 创建日期：2016年12月15日下午6:13:42
	 */
	@RequestMapping(value = "/editEmployeeJsp", method = RequestMethod.GET)
	public ModelAndView editEmployeeJsp(HttpServletRequest request,Integer eid)  {
		ModelAndView mv = new ModelAndView();
		Employee employee = employeeService.selectById(eid);
		mv.addObject("employee", employee);
		mv.setViewName("employee/editemployee");
		return mv;		
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：修改员工信息 
	 * 创建日期：2016年12月15日下午6:13:42
	 */
	@RequestMapping(value = "/editEmployee", method = RequestMethod.POST)
	public ModelAndView editEmployee(HttpServletRequest request,Employee employee)  {
		ModelAndView mv = new ModelAndView();
		employeeService.update(employee);
		request.setAttribute("successInfo", "修改员工信息成功！");
		request.setAttribute("page_code", "allemployee");
		mv.setViewName("/success-info");
		return mv;		
	}

}
